package com.hrg.rds.biz.oc.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.hrg.rds.biz.comm.ConfigService;
import com.hrg.rds.biz.config.RdsConfiguration;
import com.hrg.rds.biz.oc.domain.SmartIO;
import com.hrg.rds.biz.oc.domain.SmartIOVo;
import com.hrg.rds.biz.oc.mapper.SmartIOMapper;
import com.hrg.rds.biz.oc.service.SmartIOService;
import com.hrg.rds.biz.robot.domain.Agv;
import com.hrg.rds.biz.robot.domain.AgvStatus;
import com.hrg.rds.biz.robot.domain.AgvVo;
import com.hrg.rds.biz.robot.mapper.AgvMapper;
import com.hrg.rds.biz.robot.service.AgvService;
import com.hrg.rds.biz.station.domain.Segment;
import com.hrg.rds.biz.station.domain.Station;
import com.hrg.rds.biz.station.mapper.SegmentMapper;
import com.hrg.rds.biz.station.mapper.StationMapper;
import com.hrg.rds.biz.waybill.domain.template.cmd.CmdParam;
import com.hrg.seer.sdk.rbk.msg.config.ConfigDownloadMapRes;
import com.hrg.sys.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class SmartIOServiceImpl implements SmartIOService {

    @Autowired
    private SmartIOMapper smartIOMapper;

    @Override
    public List<SmartIOVo> selectList() {
        List<SmartIOVo> smartIOVos = null;
        List<SmartIO> smartIOS = smartIOMapper.selectList();
        if (CollUtil.isNotEmpty(smartIOS)) {
            smartIOVos = new ArrayList<>();
            for (SmartIO smartIO : smartIOS) {
                SmartIOVo smartIOVo = new SmartIOVo();
                smartIOVo.setId(smartIO.getId());
                smartIOVo.setProtocolType(smartIO.getProtocolType());
                smartIOVo.setOpenParams(JSONUtil.toList(smartIO.getOpenParams(), CmdParam.class));
                smartIOVo.setCloseParams(JSONUtil.toList(smartIO.getCloseParams(), CmdParam.class));
                smartIOVo.setOpenSegments(JSONUtil.toList(smartIO.getOpenSegments(), String.class));
                smartIOVo.setCloseSegments(JSONUtil.toList(smartIO.getCloseSegments(), String.class));
                smartIOVo.setCreateTime(smartIO.getCreateTime());
                smartIOVo.setCreateBy(smartIO.getCreateBy());
                smartIOVo.setUpdateTime(smartIO.getUpdateTime());
                smartIOVo.setUpdateBy(smartIO.getUpdateBy());
                smartIOVo.setRemark(smartIO.getRemark());
                smartIOVos.add(smartIOVo);
            }
        }
        return smartIOVos;
    }

    @Override
    public void saveOrUpdate(SmartIOVo smartIOVo) {
        SmartIO smartIO = new SmartIO();
        boolean isInsert = false;
        if (StrUtil.isBlank(smartIOVo.getId())) {
            isInsert = true;
            smartIOVo.setId(IdUtil.getSnowflakeNextIdStr());
            Date now = new Date();
            smartIO.setCreateTime(now);
            //smartIO.setCreateBy(SecurityUtils.getUsername());
            smartIO.setUpdateTime(now);
            //smartIO.setUpdateBy(SecurityUtils.getUsername());

        } else {
            smartIO.setId(smartIOVo.getId());
            Date now = new Date();
            smartIO.setUpdateTime(now);
            //smartIO.setUpdateBy(SecurityUtils.getUsername());
        }
        smartIO.setProtocolType(smartIOVo.getProtocolType());
        smartIO.setOpenParams(JSONUtil.toJsonStr(smartIOVo.getOpenParams()));
        smartIO.setCloseParams(JSONUtil.toJsonStr(smartIOVo.getCloseParams()));
        smartIO.setOpenSegments(JSONUtil.toJsonStr(smartIOVo.getOpenSegments()));
        smartIO.setCloseSegments(JSONUtil.toJsonStr(smartIOVo.getCloseSegments()));
        smartIO.setRemark(smartIOVo.getRemark());
	    if (isInsert) {
		    smartIOMapper.insert(smartIO);
	    } else {
		    smartIOMapper.updateById(smartIO);
	    }
    }
}
